// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/questionnaire/api/IbQuestionnaireApi'

import OneClass from '@api/questionnaire/model/QuestionnaireAnswerReq'
import SubmitClass from '@api/questionnaire/model/QuestionnaireSubmitReq'
import ChildClass from '@api/questionnaire/model/QuestionnaireSubmitDto'

import UTIL from '@admin/views/Operation/detail/DetailQuestionnaire/store/answer/UTIL'
import API from '@admin/views/Operation/Questionnaire/QuestionnaireList/store/API'

const CLIENT = CORE.grpcCLIENT(Client)
const CHILDREN = [ChildClass, 'dtos', 1]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 信息 —————————————————————————————— //

  getInfo: API.getOne,
  transInfo: API.transOne,

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id, answerId }, TIP = '查询单项') {
    if (!answerId) return {}

    const DTO = CORE.grpcDTO(OneClass, { questionnaireId: id, answerId })
    const { dtos = [] } = await CORE.grpc(TIP, CLIENT, 'questionnaireAnswer', DTO)
    return dtos[0] || {}
  },
  transOne(obj, { state: { info: { id, questionList }, infoParam: { answerId, instanceId } } }) {
    const one = { id, answerId, instanceId, answer: obj }
    UTIL.setAnswer(one, questionList, obj.dtos)
    return one
  },

  // —————————————————————————————— 提交 —————————————————————————————— //

  transSubmit(one, { state: { info: { id: questionnaireId, modifyTime, questionList } } }) {
    const dtos = []

    questionList.forEach(({ id, no, type, optionList, questionList: subList }) => {
      const obj = { questionnaireId, parentQuestionId: '', parentNo: 0, questionId: id, no }
      const trans = (label) => {
        const option = optionList.find((option) => option.label === label) || {}
        return { optionId: option.id }
      }

      if (subList.length) { // 有子问题的
        subList.forEach(({ id: subId, no: subNo, questionList: subSubList }) => {
          const subObj = { ...obj, parentQuestionId: id, parentNo: no, questionId: subId, no: subNo }
          if (type === 'textareaTable') { // 表格问答
            subSubList.forEach(({ id: subSubId, no: subSubNo }) => {
              const value = one[`no_${no}_${subSubNo}`]
              value && dtos.push({ ...subObj, value, questionId: subSubId, no: subSubNo })
            })
          } else { // 表格单选、多选
            const value = one[`no_${no}_${subNo}`]
            if (!value) return

            if (type === 'radioTable') dtos.push({ ...subObj, ...trans(value) })
            if (type === 'checkboxTable') value.split(' ').forEach((x) => dtos.push({ ...subObj, ...trans(x) }))
          }
        })
      } else { // 无子问题的
        const value = one[`no_${no}`]
        if (!value) return

        if (type === 'radio') dtos.push({ ...obj, ...trans(value) })
        if (type === 'checkbox') value.split(' ').forEach((x) => dtos.push({ ...obj, ...trans(x) }))
        if (type === 'textarea') dtos.push({ ...obj, value })
        if (type === 'rate') dtos.push({ ...obj, value })
      }
    })

    return { answerId: one.answerId, instanceId: one.instanceId, questionnaireId, modifyTime, dtos }
  },

  async submit(one, TIP = '提交') {
    const DTO = CORE.grpcDTO(SubmitClass, one, CHILDREN)
    await CORE.grpc(TIP, CLIENT, 'questionnaireSubmit', DTO)
    return CORE.grpcOK(TIP)
  },
}
